using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Emlyn.Website.Users;
using System.Net.Mail;
using Emlyn.Common.Data.DBUpdater;

public partial class UpdateUser : EmlynPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DBUpdaterMain.WebsiteCode = "EmlynORegan";

        if (!IsPostBack)
        {
            string luserName = Page.User.Identity.Name;

            if (String.IsNullOrEmpty(luserName))
                Response.Redirect("default.aspx");

            User luser = (new UserQuery()).GetUserByUserName(luserName);

            txtUsername.Text = luser.UserName;
            txtPassword.Text = String.Empty;
            txtPassword2.Text = String.Empty;
            txtEmail.Text = luser.Email;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtPassword.Text != txtPassword2.Text)
                lblError.Text = "Passwords do not match";
            else if (String.IsNullOrEmpty(txtUsername.Text))
                lblError.Text = "Please provide a username";
            else if (String.IsNullOrEmpty(txtEmail.Text))
                lblError.Text = "Please provide an email address";
            else
            {
                EWebsite lwebsite = new EWebsite(DBUpdaterMain.WebsiteCode);

                MailAddress lemail = new MailAddress(txtEmail.Text);
                // throw an exception if the email address is broken

                User luser = (new UserQuery()).GetUserByUserName(Page.User.Identity.Name);

                if (!String.IsNullOrEmpty(txtPassword.Text))
                    luser.SetPassword(txtPassword.Text);
                luser.Email = txtEmail.Text;

                (new UserMod()).Save(luser);

                //System.Net.Mail.SmtpClient lclient = new SmtpClient("smtp.gmail.com", 587);

                //MailMessage lmessage = new MailMessage(
                //    "emlynoregan@gmail.com", txtEmail.Text, "Validate Registration with emlynoregan.com",
                //    "Hey, thanks for registering! You need to validate your registration, which is easy! Just " +
                //    "go here: http://emlynoregan.com/ValidateUser.aspx?user=" + luser.UserName + "+code=" + luser.ValidationCode + " ." + Environment.NewLine +
                //    "---" + Environment.NewLine +
                //    "Emlyn" + Environment.NewLine +
                //    Environment.NewLine +
                //    "emlynoregan.com"
                //);

                //lclient.Credentials = new System.Net.NetworkCredential("emlynoregan@gmail.com", "xylmeonline");
                //lclient.EnableSsl = true;
                //lclient.Send(lmessage);

                //FormsAuthentication.RedirectFromLoginPage(luser.UserName, false);

                lblError.Text = "Changes applied successfully";
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.GetType().Name + ": " + ex.Message;
        }
    }
    protected void btnSubmit_Load(object sender, EventArgs e)
    {

    }
}
